package com.gsdse.service.impl;


import com.gsdse.dao.DepartmentDao;
import com.gsdse.pojo.Department;
import com.gsdse.pojo.PageResult;
import com.gsdse.pojo.ReturnData;
import com.gsdse.pojo.User;
import com.gsdse.service.ApplyService;
import com.gsdse.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.AbstractMap;
import java.util.List;
import java.util.Map;


@Service
public class DepartmentServicelmpl implements DepartmentService {


    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public Boolean updateDepartmentById(Department department) {
        Boolean update = departmentDao.updateDepartmentById(department);
        return update;
    }

    @Override
    public Department selectDepartmentById(Integer departmentId) {
        Department department = departmentDao.selectDepartmentById(departmentId);
        return department;
    }

    @Override
    public ReturnData selectMembers(Integer departmentId, Integer page, Integer pageSize) {
        int start = (page - 1) * pageSize;
        List<User> users = departmentDao.selectMembers(departmentId, start, pageSize);
        //封装查询结果
        PageResult pageResult = new PageResult();
        pageResult.setTotal(users.size());
        pageResult.setData(users);

        ReturnData returnData = new ReturnData();
        returnData.setData(pageResult);
        returnData.setMsg("查询成功！");
        returnData.setCode(0);
        return returnData;
    }

    @Override
    public ReturnData getTotal(Integer departmentId) {

        int total = departmentDao.selectMemberCount(departmentId);
        ReturnData returnData = new ReturnData();
        returnData.setData(total);
        returnData.setCode(0);
        returnData.setMsg("查询成功！");
        return returnData;
    }

    @Override
    public ReturnData removeUser(Integer userId,String departmentId) {
        boolean isOk = departmentDao.removeUser(userId);
        ReturnData returnData = new ReturnData();
        if(isOk) {
            AbstractMap.SimpleEntry<String, Object> remover = new AbstractMap.SimpleEntry<>("3", userId.toString()+","+departmentId);
            ApplyServiceImpl.applyQueue.add(remover);
            returnData.setCode(0);
            returnData.setMsg("移除成功！");
            return returnData;
        }
        else {
            returnData.setCode(1);
            returnData.setMsg("移除失败!");
            return returnData;
        }

    }


}

